package view;

import service.FileService;
import service.MessageService;
import service.UserClientService;
import utils.PrintUtil;

/**
 * @author 余一
 * @date 2021/10/25  10:50
 */
public class QQView {

    private boolean loop = true; //控制是否显示菜单
    private String key = ""; //接收用户的键盘输入
    private UserClientService userClientService = new UserClientService();//用于登录退出等服务
    private MessageService messageService = new MessageService();//用于管理消息传递的业务
    private FileService fileService = new FileService();//用于管理文件传输的业务

    //显示主菜单
    public void mainMenu(){
        while (loop){

            System.out.println("==========欢迎登录网络通信系统==========");
            System.out.println("\t\t 1 登录系统");
            System.out.println("\t\t 9 退出系统");
            System.out.print("请选择菜单：");

            key = PrintUtil.readString(1);

            switch (key){
                case "1" :
                    System.out.print("请输入用户名：");
                    String userId = PrintUtil.readString(20);
                    System.out.print("请输入密码：");
                    String password = PrintUtil.readString(20);
                    //将用户名和密码提交到服务端去验证该用户是否合法
                    if (userClientService.checkUser(userId,password)){
                        while (loop){
                            System.out.println("==========欢迎("+userId+")进入网络通信系统菜单==========");
                            System.out.println("\t\t 1 显示在线列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私聊消息");
                            System.out.println("\t\t 4 发送文件");
                            System.out.println("\t\t 9 退出系统");
                            System.out.print("请选择菜单：");

                            key = PrintUtil.readString(1);
                            switch (key){
                                case "1":
                                    userClientService.onlineUser();
                                    break;
                                case "2":
                                    System.out.println("请输入群发的消息：");
                                    String content = PrintUtil.readString(100);
                                    messageService.massTexting(content,userId);
                                    break;
                                case "3":
                                    System.out.println("请输入接收消息的用户：");
                                    String receiver = PrintUtil.readString(9);
                                    System.out.println("请输入传送的消息：");
                                    String msg = PrintUtil.readString(60);
                                    messageService.privateChat(msg,userId,receiver);
                                    break;
                                case "4":
                                    System.out.println("发送文件");
                                    System.out.println("请指定要接收文件的用户：");
                                    String order = PrintUtil.readString(20);
                                    System.out.println("请输入该文件的路径：");
                                    String src = PrintUtil.readString(100);
                                    System.out.println("请输入对方的保存路径：");
                                    String destination = PrintUtil.readString(100);
                                    fileService.sendOne(src,destination,userId,order);
                                    break;
                                case "9":
                                    userClientService.logout();
                                    loop = false;
                                    break;
                            }
                        }
                    }else {
                        System.out.println("用户名或密码错误，登录失败");
                    }
                    break;
                case "9" :
                    loop = false;
                    break;
            }
        }
    }
}
